package com.example.actearly_revised;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.example.actearly_revised.QuestionsObject.Node;
import com.example.actearly_revised.R;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;

@TargetApi(11)
public class MilestoneTracker extends Activity {

	String question[];
	String keys[];
	Button next_button;
	QuestionsObject db;
	Node question_node;
	SQLogic conn;
	TextView currentQuestion;
	Button nextButton;
	Button backButton;
	ImageButton leftButton;
	ImageButton rightButton;
	TextView currentAge;
	RadioGroup buttonGroup;
	RadioButton greenButton ;
	RadioButton redButton ;
	RadioButton yellowButton ;
	int clickCounter;
	String temp;
	int ageCounter;
	static ArrayList<String> holdValue = new ArrayList<String>();
	String[] ageGroups;
	int[] ageID;
	ProgressBar percentDone;
	Intent p_logic;
	int profile_number;
	final String yes_ans = "yes";
	public boolean On_Change_Of_Answer = false;
	final String no_ans = "no";
	final String maybe_ans = "notsure";
	static RadioButtonHelper boolhelp;
	static LinkedList<Integer> flag = new LinkedList<Integer>();
	TabHomePage parent;
	ImageButton notebutton;
	ImageButton exampleButton;
	static int currmilestone;
	static int count = 0;
	static HashMap<Integer, Integer> milestoneMap = new HashMap<Integer, Integer>();
	Creating_Node_List newn = new Creating_Node_List(18, MilestoneTracker.this);
	@Override
	public void onCreate(Bundle savedInstanceState){
		System.out.println("testing1");
		conn = new SQLogic(MilestoneTracker.this);
		super.onCreate(savedInstanceState);
		//Creating_Node_List newn = new Creating_Node_List(18, MilestoneTracker.this);
		newn.add(18);
		setContentView(R.layout.activity_milestone_tracker);
		RelativeLayout pageLayout=(RelativeLayout)findViewById(R.id.trackerLayout);
		//pageLayout.setBackgroundResource(R.drawable.background);
		leftButton = (ImageButton) findViewById(R.id.leftButton);
		rightButton = (ImageButton) findViewById(R.id.rightButton);
		currentAge= (TextView) findViewById(R.id.currentAge);
		currentAge.setText("18 months");
		nextButton = (Button) findViewById(R.id.next_button); 
		nextButton.setVisibility(View.INVISIBLE);
		notebutton = (ImageButton) findViewById(R.id.bttnAddNote);
		exampleButton = (ImageButton) findViewById(R.id.bttnExamplePic);

		//font change
		Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/futuram.ttf");
		CheckedTextView Text = (CheckedTextView) findViewById(R.id.yesLabel);
		CheckedTextView Text1 = (CheckedTextView) findViewById(R.id.noLabel);
		CheckedTextView Text2 = (CheckedTextView) findViewById(R.id.maybeLabel);
		TextView Text3 = (TextView) findViewById(R.id.milestoneText);
		TextView Text4 = (TextView) findViewById(R.id.currentAge);


		Text1.setTypeface(customFont);
		Text.setTypeface(customFont);
		Text2.setTypeface(customFont);
		Text3.setTypeface(customFont);
		Text4.setTypeface(customFont);
		//notebutton.setTypeface(customFont);

		this.profile_number = parent.profile_number;
		boolhelp = new RadioButtonHelper();
		flag.addFirst(0);

		conn.open();
		//need to add code to retrieve ANSWERS as well
		System.out.println(">>>>>>>>>>>>>>>>>>>LLLLLLLLLLLLLL"+profile_number);
		db = conn.RetrieveAllQuestions(18,profile_number);
		conn.close();

		// change this if you change the default age group
		parent = (TabHomePage) this.getParent();
		parent.currentmilestone = 18;
		
		currmilestone = parent.currentmilestone;

		question = new String[db.count()];
		keys = new String[db.count()];
		db.getMap().keySet().toArray(keys); // this is needed because the keys might not necessarily be in order
		//

		//		keys aren't actually sorted in order, so need to do that here		
		int keyInt[]= new int[db.count()];

		for (int j=0; j<db.count();j++){
			keyInt[j]=Integer.parseInt(keys[j]);
		}
		Arrays.sort(keyInt);
		for (int j=0; j<db.count();j++){
			keys[j]=Integer.toString(keyInt[j]);
		}




		for (int j=0; j<db.count(); j++)  
		{
			String tempKey = keys[j];
			Node tempQuestion=db.getQuestions(tempKey);
			question[j]= tempQuestion.Question;
		}
		currentQuestion = (TextView) findViewById(R.id.milestoneText);
		currentQuestion.setText(question[0]);
		//nextButton = (Button) findViewById(R.id.nextButton);
		backButton = (Button) findViewById(R.id.backButton);
		if(Back_Stack.count <= 0){
			backButton.setVisibility(View.INVISIBLE);
		}
		//Custom_Timer.Custom_Timer();
		buttonGroup= (RadioGroup) findViewById(R.id.buttonGroup);
		greenButton = (RadioButton)findViewById(R.id.greenButton);
		redButton = (RadioButton)findViewById(R.id.redButton);
		yellowButton =(RadioButton)findViewById(R.id.yellowButton);
		//keeps track of which page person is on
		clickCounter=0;
		//keeps track of which age group is being viewed
		ageCounter=5;
		ageID = new int[] {
				2,4,6,9,12,18,24,36,48,60
		};
		//create progress bar
		percentDone= (ProgressBar) findViewById(R.id.milestoneProgress);
		// reset the progress bar to the default value of 0
		percentDone.setProgress(0);
		//set the maximum value of progress bar based on # of questions
		percentDone.setMax(Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1);
		ageGroups= getResources().getStringArray(R.array.milestoneAges);

		//check if right/left arrows have been pushed, then update milestones as needed
		rightButton.setOnClickListener(new View.OnClickListener() { 

			public void onClick(View v) {
				if (ageCounter<ageGroups.length-1)
					ageCounter++;
				//Back_Stack.clear();
				currentAge.setText(ageGroups[ageCounter]);
				conn = new SQLogic(MilestoneTracker.this);
				conn.open();
				QuestionsObject db = conn.RetrieveAllQuestions(ageID[ageCounter],0);
				conn.close();
				question= new String[db.count()];
				String keys[] = new String[db.count()];
				db.getMap().keySet().toArray(keys); // this is needed because the keys might not necissarily be in order
				for (int j=0; j<db.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
				{
					Node question_node = db.getQuestions(keys[j]);
					question[j]= question_node.Question;
				}
				clickCounter=0;
				currentQuestion.setText(question[clickCounter]);
				parent.currentmilestone = ageID[ageCounter];
				currmilestone = parent.currentmilestone;

				if(!Creating_Node_List.milestone_map.containsKey(parent.currentmilestone)){
					newn.add(parent.currentmilestone);
				}
				percentDone.setMax(Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1);
				//currentQuestion.setText(Creating_Node_List.milestone_map.get(ageGroups[ageCounter]).get(0).getQuestion());
				percentDone.setProgress(newn.getCountOfMilestone(parent.currentmilestone));

				int answer_temp = 3;
				//if(newn.getCountOfMilestone(parent.currentmilestone) > 0){
				//newn.decrement(parent.currentmilestone);
				System.out.println(">>>>>>>"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				boolean temp_answered = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).flag_answered;
				if(temp_answered == true){
					answer_temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer(); 
				}
				System.out.println("}}}}}}}}}}}}}}}}}}}"+answer_temp);
				//Back_Stack.backBoolean = true;
				//}
				System.out.println(answer_temp);
				if(answer_temp == 1){
					System.out.println("green");
					greenButton.setChecked(true);
					return;
				}
				else if(answer_temp == 0){
					System.out.println("SDFASDF");
					redButton.setChecked(true);
					return;
				}
				else if(answer_temp == 2){
					yellowButton.setChecked(true);
					return;
				}
				else if(answer_temp == 3){
					yellowButton.setChecked(false);
					redButton.setChecked(false);
					greenButton.setChecked(false);
				}
			}
		});

		leftButton.setOnClickListener(new View.OnClickListener() { 

			public void onClick(View v) {
				if (ageCounter>0)
					ageCounter--;
				//Back_Stack.clear();
				currentAge.setText(ageGroups[ageCounter]);
				conn = new SQLogic(MilestoneTracker.this);

				conn.open();
				QuestionsObject db = conn.RetrieveAllQuestions(ageID[ageCounter],profile_number);
				conn.close();
				question= new String[db.count()];
				String keys[] = new String[db.count()];
				db.getMap().keySet().toArray(keys); // this is needed because the keys might not necissarily be in order
				for (int j=0; j<db.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
				{
					Node question_node = db.getQuestions(keys[j]);
					question[j]= question_node.Question;
				}
				clickCounter=0;
				//currentQuestion.setText(question[clickCounter]);
				parent.currentmilestone = ageID[ageCounter];
				if(!Creating_Node_List.milestone_map.containsKey(parent.currentmilestone)){
					newn.add(parent.currentmilestone);
				}
				percentDone.setMax(Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1);
				percentDone.setProgress(newn.getCountOfMilestone(parent.currentmilestone));


				System.out.println("{{{{{{{{" + parent.currentmilestone +"}}}}}}}" );
				currmilestone = parent.currentmilestone;

				int answer_temp = 3;
				//if(newn.getCountOfMilestone(parent.currentmilestone) > 0){
				//newn.decrement(parent.currentmilestone);
				System.out.println(">>>>>>>"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				boolean temp_answered = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).flag_answered;
				if(temp_answered == true){
					answer_temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer(); 
				}
				System.out.println("}}}}}}}}}}}}}}}}}}}"+answer_temp);
				//Back_Stack.backBoolean = true;
				//}
				System.out.println(answer_temp);
				if(answer_temp == 1){
					System.out.println("green");
					greenButton.setChecked(true);
					return;
				}
				else if(answer_temp == 0){
					System.out.println("SDFASDF");
					redButton.setChecked(true);
					return;
				}
				else if(answer_temp == 2){
					yellowButton.setChecked(true);
					return;
				}
				else if(answer_temp == 3){
					yellowButton.setChecked(false);
					redButton.setChecked(false);
					greenButton.setChecked(false);
				}
			}
		});


		temp = (Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
		redButton.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				//System.out.println();
				temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion();
				Creating_Node_List.change(parent.currentmilestone, temp, 0, MilestoneTracker.this, profile_number);
				System.out.println(("11111111111111111"+temp));
				System.out.println("PPPPPPPP"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer());
				Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).setFlag();
				System.out.println("+++++++++++++++++++++"+newn.getCountOfMilestone(parent.currentmilestone));
				System.out.println("++++++++++++++++++++++++++++++++++++"+(Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1));
				if(newn.getCountOfMilestone(parent.currentmilestone) == Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1){
					nextButton.setVisibility(View.INVISIBLE);
					CharSequence text = "Milestone Completed";
					Context context = getApplicationContext();
					int duration = Toast.LENGTH_SHORT;
					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
					return;
				}
				newn.increment(parent.currentmilestone);
				System.out.println("R");
				Custom_Timer.Custom_Timer();
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				percentDone.setProgress(newn.getCountOfMilestone(parent.currentmilestone));
				if(newn.getCountOfMilestone(parent.currentmilestone) > 0){
					backButton.setVisibility(View.VISIBLE);
				}
				System.out.println("LLLLLL"+newn.getCountOfMilestone(parent.currentmilestone));
				if(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).flag_answered == false){
					System.out.println(">>>>>>>>>>>>>>" +(newn.getCountOfMilestone(parent.currentmilestone)));
					System.out.println("::::::::::::"+((Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1)));
					if(!(newn.getCountOfMilestone(parent.currentmilestone) == Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1)){
						redButton.setChecked(false);
						yellowButton.setChecked(false);
						greenButton.setChecked(false);
					}
				}
			}

		});
		greenButton.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion();
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				Creating_Node_List.change(parent.currentmilestone, temp, 1, MilestoneTracker.this, profile_number);
				System.out.println("PPPPPPPP"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer());
				Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).setFlag();
				if(newn.getCountOfMilestone(parent.currentmilestone) == Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1){
					nextButton.setVisibility(View.INVISIBLE);
					CharSequence text = "Milestone Completed";
					Context context = getApplicationContext();
					int duration = Toast.LENGTH_SHORT;
					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
					return;
				}
				newn.increment(parent.currentmilestone);
				System.out.println("G");
				Custom_Timer.Custom_Timer();
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				backButton.setVisibility(View.VISIBLE);
				percentDone.setProgress(newn.getCountOfMilestone(parent.currentmilestone));
				if(newn.getCountOfMilestone(parent.currentmilestone) > 0 ){
					backButton.setVisibility(View.VISIBLE);
				}

				System.out.println("LLLLLL"+newn.getCountOfMilestone(parent.currentmilestone));
				if(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).flag_answered == false){
					redButton.setChecked(false);
					yellowButton.setChecked(false);
					greenButton.setChecked(false);
				}
				//redButton.setChecked(false);
				//yellowButton.setChecked(false);
				//greenButton.setChecked(false);
			}

		});
		yellowButton.setOnClickListener(new View.OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
				temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion();
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				Creating_Node_List.change(parent.currentmilestone, temp, 2, MilestoneTracker.this, profile_number);
				Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).setFlag();
				System.out.println("PPPPPPPP"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer());
				if(newn.getCountOfMilestone(parent.currentmilestone) == Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1){
					nextButton.setVisibility(View.INVISIBLE);
					CharSequence text = "Milestone Completed";
					Context context = getApplicationContext();
					int duration = Toast.LENGTH_SHORT;
					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
					return;
				}
				newn.increment(parent.currentmilestone);
				System.out.println("Y");
				Custom_Timer.Custom_Timer();
				currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
				backButton.setVisibility(View.VISIBLE);
				percentDone.setProgress(newn.getCountOfMilestone(parent.currentmilestone));
				if(newn.getCountOfMilestone(parent.currentmilestone) > 0){
					backButton.setVisibility(View.VISIBLE);
				}
				System.out.println("LLLLLL"+count);
				if(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).flag_answered == false){
					redButton.setChecked(false);
					yellowButton.setChecked(false);
					greenButton.setChecked(false);
				}
				//redButton.setChecked(false);
				//yellowButton.setChecked(false);
				//greenButton.setChecked(false);

			}



		});


		/*RadioButton checkedRadioButton = (RadioButton)buttonGroup.findViewById(buttonGroup.getCheckedRadioButtonId());

		buttonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
		{			

			public void onCheckedChanged(RadioGroup bGroup, int checkedId)
			{
				long start = System.currentTimeMillis();
				boolean go = false;
				boolean cont = false;
				System.out.println(Back_Stack.backBoolean);
				backButton.setVisibility(View.VISIBLE);
				if(Back_Stack.backBoolean == true){
					Back_Stack.backBoolean = false;
					return;
				}
				if(bGroup.isPressed()){
					//continue;

					System.out.println("pressed");
					return;
				}

				//if()
				//Back_Stack.pushQuestion(question_input)
				System.out.println("LLLLLLLLLLLLL" + holdValue);
				if(!holdValue.contains(question[clickCounter])){
					holdValue.add(question[clickCounter]);
				}

				System.out.println("ADSFASDFADSF"+holdValue);
				if(Back_Stack.backBoolean == true){
					if(!bGroup.isPressed()){
						System.out.println("PPPPPPPPPPPPPP");
						return;
					}
				}
				System.out.println("MMMMMMMMMMMMMMMMMMMMMM"+Back_Stack.questionList);

				while(!go){
					if(System.currentTimeMillis() - start >= 1*200){
						go = true;
						cont = true;
					}
				}
				if(cont == true){
					System.out.println("the boolean flag is returning "
							+ RadioButtonHelper.toSuppress);
					int stackEntry = flag.removeFirst();
					System.out.println("the entry removed from the stack was a: " + stackEntry);

					if (stackEntry == 0) {
						RadioButton checkedRadioButton = (RadioButton) bGroup
								.findViewById(checkedId);

						int q_id = Integer.parseInt(keys[clickCounter]);
						conn.open();
						String temp = (Creating_Node_List.milestone_map.get(18).get(count).getQuestion());
						if (checkedRadioButton == greenButton) {
							Creating_Node_List.change(18, temp, 0);
							count++;

						} else if (checkedRadioButton == redButton) {
							Creating_Node_List.change(18, temp, 1);
							count++;

						} else if (checkedRadioButton == yellowButton) { 
							Creating_Node_List.change(18, temp, 2);
							count++;

						}
						conn.close();

						if (checkedId != -1) {
							if (clickCounter < test.count() - 1) {
								clickCounter++;
							}

							// checkedRadioButton.setBackgroundColor(0xffcccccc);

		 * buttonGroup.setClickable(true); Handler
		 * handler= new Handler();
		 * handler.postDelayed(new Runnable(){ public
		 * void run(){ } }, 1500);
		 * System.out.println(checkedId);


							percentDone.setProgress(clickCounter);
							try{
								currentQuestion.setText(question[clickCounter]);
							}
							catch(Exception e){
								clickCounter--;

							}
							currentQuestion.setText(Creating_Node_List.milestone_map.get(18).get(count).getQuestion());

		 * To get the checked RadioButton from the
		 * RadioGroups you do: int checkedId =
		 * bGroup.getCheckedRadioButtonId();

							flag.addFirst(0);
							bGroup.clearCheck();
						}
					}
					flag.addFirst(0);
				}
				if (clickCounter==test.count()-1)
				{
					switchTab(2);
				}
			}
		});*/


		backButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				//Timer newTimer = new Timer(
				int answer_temp = 0;
				System.out.println("!!!!!!!!!!!!!"+newn.getCountOfMilestone(parent.currentmilestone));
				nextButton.setVisibility(View.VISIBLE);
				if(newn.getCountOfMilestone(parent.currentmilestone) > 0){
					newn.decrement(parent.currentmilestone);
					System.out.println(">>>>>>>"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
					currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
					answer_temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer(); 
					System.out.println("}}}}}}}}}}}}}}}}}}}"+answer_temp);
					//Back_Stack.backBoolean = true;
				}
				System.out.println(answer_temp);
				if(answer_temp == 1){
					System.out.println("green");
					greenButton.setChecked(true);
					return;
				}
				else if(answer_temp == 0){
					System.out.println("SDFASDF");
					redButton.setChecked(true);
					return;
				}
				else if(answer_temp == 2){
					yellowButton.setChecked(true);
					return;
				}

				/*	if(Back_Stack.questionList.get(0).equals(temp)){
					Back_Stack.backBoolean = false;
					backButton.setVisibility(View.INVISIBLE);
				}*/
				/*
				if(Back_Stack.storeCount == true){

					return;
				}

				//if(((Object) newTimer).getDelay() == 2)
				Context context = getApplicationContext();

				if (clickCounter!=0){
					clickCounter--;
				}
				buttonGroup.clearCheck();
				String temp = "";
				try {
					temp = Back_Stack.popQuestion();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("???????????????????" + temp);

				currentQuestion.setText(temp);
				System.out.println("SSSSSSSSSSSSSSS");
				//currentQuestion.setText(question[clickCounter]);
				System.out.println(clickCounter);

				String question_id = keys[clickCounter];
				if(Back_Stack.temp != Back_Stack.count){
					nextButton.setVisibility(View.VISIBLE);
					On_Change_Of_Answer = true;
				}

				int answer_temp = 0;
				try {
					answer_temp = Back_Stack.popAnswer();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(answer_temp == 0){
					greenButton.setChecked(true);
				}
				else if(answer_temp == 1){
					redButton.setChecked(true);
				}
				else if(answer_temp == 2){
					yellowButton.setChecked(true);
				}

				if(Back_Stack.questionList.get(0).equals(temp)){
					Back_Stack.backBoolean = false;
					backButton.setVisibility(View.INVISIBLE);
				}
				System.out.println("++++++++"+Back_Stack.count);*/
				/*	int q_id = Integer.parseInt(question_id);

				System.out.println("this better be the same as the save ID: " + q_id);

				AnswersObject prevAns;
				conn.open();
				prevAns = conn.RetrieveAnswers(profile_number,q_id);
				conn.close();	

				String[] stupid_keys = new String[prevAns.count()];
				prevAns.getMap().keySet().toArray(stupid_keys);

				String ans = prevAns.get(question_id);
				System.out.println("the answer to this question is: " + ans);

				flag.addFirst(1);*/
				/*while(Back_Stack.temp != Back_Stack.count){
					nextButton.setVisibility(View.VISIBLE);
				}
				if(ans.equals(yes_ans))
					greenButton.setChecked(true);
				else if(ans.equals(no_ans))
					redButton.setChecked(true);
				else if(ans.equals(maybe_ans))
					yellowButton.setChecked(true);

				flag.addFirst(0);

				System.out.println("this is the back button, the boolean flag is " + RadioButtonHelper.toSuppress);
				percentDone.setProgress(clickCounter);*/
			}
			/*to set the button that was previously clicked (if any)
				 retrieve input from database
				  convert value to desiredId
				 bGroup.setId(desiredId);
			 */

		});


		nextButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				int answer_temp = 0;
				System.out.println("!!!!!!!!!!!!!"+newn.getCountOfMilestone(18));

				if(newn.getCountOfMilestone(parent.currentmilestone) < Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1){
					newn.increment(parent.currentmilestone);
					System.out.println(">>>>>>>"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
					currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
					answer_temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer(); 
					System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFF"+newn.getCountOfMilestone(parent.currentmilestone));
					System.out.println("FFFFFFFFFFFFFFFFFFFFFFFFF"+(Creating_Node_List.gettingLastEditedAnswer(parent.currentmilestone)-1));
					if(newn.getCountOfMilestone(parent.currentmilestone) > Creating_Node_List.gettingLastEditedAnswer(parent.currentmilestone)){

						nextButton.setVisibility(View.INVISIBLE);
						greenButton.setChecked(false);
						redButton.setChecked(false);
						yellowButton.setChecked(false);
						return;
					}
				}

				if(answer_temp == 1){
					greenButton.setChecked(true);
					return;
				}
				else if(answer_temp == 0){
					redButton.setChecked(true);
					return;
				}
				else if(answer_temp == 2){
					yellowButton.setChecked(true);
					return;
				}
			}
			/*		System.out.println("555555555555555"+holdValue);
				try {
					Back_Stack.nextbutton();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("%%%%%%%"+Back_Stack.count);
				if(Back_Stack.temp == Back_Stack.count+1){
					On_Change_Of_Answer= false;
					nextButton.setVisibility(View.INVISIBLE);
				}
				String temp = "";
				try {
					temp = Back_Stack.getNext();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("???????????????????question list" + Back_Stack.questionList);
				System.out.println("@@@@@@@@@@@@@@@@@@hold"+ holdValue);
				System.out.println("???????????????????" + Back_Stack.questionList.get(Back_Stack.questionList.size()-1));
				if(temp.equals("reload")){
					System.out.println(Back_Stack.questionList.get(Back_Stack.questionList.size()-1));
					String newtemp = holdValue.get(holdValue.size()-1);
					System.out.println("ASDFADFADSFADSF" +newtemp);
					currentQuestion.setText(newtemp);
					greenButton.setChecked(false);
					redButton.setChecked(false);
					yellowButton.setChecked(false);
					return;
				}
				else{
					currentQuestion.setText(temp);
				}
			}*/
		});

		notebutton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				switchTab(1);			
			}
		});

		exampleButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				//switchTab(1);		
				Example_Fragment newFragment = new Example_Fragment();
				//newFragment.show(getFragmentManager(), "missiles");

				//setContentView(R.layout.activity_example_layout);
			}
		});
	}
	@Override
	public void onResume(){
		super.onResume();
		this.profile_number = parent.profile_number;
		currmilestone = parent.currentmilestone;
		if(!Creating_Node_List.milestone_map.containsKey(parent.currentmilestone)){
			newn.add(parent.currentmilestone);
		}
		percentDone.setMax(Creating_Node_List.milestone_map.get(parent.currentmilestone).size()-1);
		percentDone.setProgress(newn.getCountOfMilestone(parent.currentmilestone));


		System.out.println("{{{{{{{{" + parent.currentmilestone +"}}}}}}}" );
		currmilestone = parent.currentmilestone;

		int answer_temp = 3;
		//if(newn.getCountOfMilestone(parent.currentmilestone) > 0){
		//newn.decrement(parent.currentmilestone);
		System.out.println(">>>>>>>"+Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
		currentQuestion.setText(Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getQuestion());
		boolean temp_answered = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).flag_answered;
		if(temp_answered == true){
			answer_temp = Creating_Node_List.milestone_map.get(parent.currentmilestone).get(newn.getCountOfMilestone(parent.currentmilestone)).getAnswer(); 
		}
		System.out.println("}}}}}}}}}}}}}}}}}}}"+answer_temp);
		//Back_Stack.backBoolean = true;
		//}
		System.out.println(answer_temp);
		if(answer_temp == 1){
			System.out.println("green");
			greenButton.setChecked(true);
			return;
		}
		else if(answer_temp == 0){
			System.out.println("SDFASDF");
			redButton.setChecked(true);
			return;
		}
		else if(answer_temp == 2){
			yellowButton.setChecked(true);
			return;
		}
		else if(answer_temp == 3){
			yellowButton.setChecked(false);
			redButton.setChecked(false);
			greenButton.setChecked(false);
		}
		/*ageCounter = contains(currmilestone,ageID);

		currentAge.setText(ageGroups[ageCounter]);
		conn = new SQLogic(MilestoneTracker.this);
		conn.open();
		QuestionsObject db = conn.RetrieveAllQuestions(ageID[ageCounter],0);
		conn.close();
		question= new String[db.count()];
		String keys[] = new String[db.count()];
		db.getMap().keySet().toArray(keys); // this is needed because the keys might not necissarily be in order
		for (int j=0; j<db.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
		{
			Node question_node = db.getQuestions(keys[j]);
			question[j]= question_node.Question;
		}
		clickCounter=0;
		//currentQuestion.setText(question[clickCounter]);
		//percentDone.setProgress(clickCounter);
*/	}

	public void switchTab(int indexTabToSwitchTo){
		TabHomePage tabhost;
		tabhost = (TabHomePage) this.getParent();
		tabhost.switchTab(indexTabToSwitchTo);

	}

	public Boolean onChangeOfAnswer(int q_id, int ans){
		if(On_Change_Of_Answer == true){
			CharSequence text = "Change of Answer";
			Context context = getApplicationContext();
			int duration = Toast.LENGTH_SHORT;	
			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
			try {
				Back_Stack.pushQuestion((String)currentQuestion.getText());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				Back_Stack.answerList.add(Back_Stack.count, ans);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		}
		return false;

	}

	// returns index of wanted int
	private int contains(int charac, int[] total){
		for(int i = 0; i < total.length; i++){
			if(total[i] == charac){
				return i;
			}
		}
		return -1;
	}

	// this loops creates 20 individual rows, each with a question and three radio buttons that are turned off
	/*for (int j=0; j<test.count();j++)  // same as in UrgetList, autoincrement starts at 1 in table
		{
			question_node = test.getQuestions(keys[j]);
			question[j]= question_node.Question;
			mSource.add(new ButtonRowObject(j, question[j], false));
		}
		conn.close(); */

	//failed attempt to dynamically create listviews that can be swiped with view pager ='(
	/*List<ListView> listviews = new ArrayList<ListView>();
		Vector<View> pages = new Vector<View>();
		for (int i=1; i<test.count();i++)
		{
			ListView listview = new ListView(mContext);
			listviews.add(listview);
			pages.add(listview);
			System.out.println("testing4");
			listviews.get(i).setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question));
		}
		System.out.println("testing");
		System.out.println(pages.size());
		ViewPager vp = (ViewPager) findViewById(R.id.milestoneViewPager);
		MilestonePageSwiper adapter = new MilestonePageSwiper(mContext,pages);
		vp.setAdapter(adapter);
		int currentPage= vp.getCurrentItem();
		TextView currentQuestion= (TextView) findViewById(R.id.milestoneText);
		currentQuestion.setText(question[currentPage]);
		System.out.println("test1");*/

	//listview.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question[currentPage]));
	//SECTION STARTS HERE
	/*ListView listview1 = new ListView(mContext);
		ListView listview2 = new ListView(mContext);
		ListView listview3 = new ListView(mContext);
		Vector<View> pages = new Vector<View>();
		pages.add(listview1);
		pages.add(listview2);
		pages.add(listview3);

		ViewPager vp = (ViewPager) findViewById(R.id.qlViewPager);
		PageSwiper adapter = new PageSwiper(mContext,pages);
		vp.setAdapter(adapter);


		listview1.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, question));
		listview2.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, new String[]{"A", "B", "C"}));
		listview3.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, new String[]{"D", "E", "F"}));
	 */
	//SECTION ENDS HERE

	//ListView mListView= (ListView) findViewById(R.id.milestoneViewPager);

	// for buttons, uncomment the line below
	//mListView.setAdapter(new RadioButtonAdapter(getApplicationContext(),mSource));

	//for list, uncomment the line below
	//setListAdapter(new ArrayAdapter<String>(MilestoneTracker.this, android.R.layout.simple_list_item_1, question));
}





/*	//specifies what each row will have
	private class RadioButtonAdapter extends ArrayAdapter<ButtonRowObject>{
		class ViewHolder {
			TextView   milestoneString;
			RadioGroup rGroup;
			RadioButton button1;
			RadioButton button2;
			RadioButton button3;
		}*/


/*private LayoutInflater mInflater;

		public RadioButtonAdapter(Context context, ArrayList<ButtonRowObject> mSource){
			super(context, R.layout.activity_button_row_layout, mSource);
			mInflater = LayoutInflater.from(context);
		}

		//sets the view to include the many rows of stuff

		public View getView(final int position, View convertView, ViewGroup parent){
			ViewHolder holder;

			if (convertView == null){
				convertView = mInflater.inflate(R.layout.activity_button_row_layout, null);

				holder = new ViewHolder();
				holder.milestoneString = (TextView) convertView.findViewById(R.id.milestoneText);
				holder.rGroup = (RadioGroup) convertView.findViewById(R.id.buttonGroup);
				holder.button1=(RadioButton) convertView.findViewById(R.id.greenButton);
				convertView.setTag(holder);
			}
			else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
				public void onCheckedChanged(RadioGroup group, int checkedId){
					switch (checkedId){
					case R.id.greenButton:
						mSource.get(position).setFirstChecked(true);
						break;

					case R.id.redButton:
						mSource.get(position).setFirstChecked(false);
						break;

					case R.id.yellowButton:
						mSource.get(position).setFirstChecked(false);
						break;
					}
				}
			});

			// code for turning off/on radio buttons depending on which one is pressed
						if (mSource.get(position).isFirstChecked()){
				holder.button1.setChecked(true);
				holder.button2.setChecked(false);
				holder.button3.setChecked(false);
			}
			else{
				holder.button1.setChecked(false);
				holder.button2.setChecked(true);
				holder.button3.setChecked(true);
			}

			//ListView mListView= (ListView) findViewById(R.id.milestoneList);
			//List<String> questionList= Arrays.asList(question);

			//holder.milestoneString.setText(mSource.get(position).getName());


			return convertView;
		}
	}
}*/

/*package com.example.actearly_revised;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.actearly_revised.QuestionsObject.Node;
import com.example.actearly_revised.R;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MilestoneTracker extends ListActivity {

	String question[];
	String keys[];
	QuestionsObject test;
	Node question_node;


	public MilestoneTracker(){
		question = new String[10];
		// test = new QuestionsObject();
	}


	public void onCreate(Bundle buttons) {
		super.onCreate(buttons);
		setContentView(R.layout.activity_milestone_tracker);
		ArrayAdapter<Model> adapter = new ButtonAdapter(this, getModel());
		setListAdapter(adapter);

	}
	private List<Model> getModel() {

		SQLogic conn = new SQLogic(MilestoneTracker.this);
		conn.open();
		test= conn.RetrieveQuestions();
		conn.close();

		question = new String[test.count()];
		keys = new String[test.count()];
		test.getMap().keySet().toArray(keys);

		for (int j=0; j<test.count(); j++)  //not gauranteed that questionId's are in order
		{
			question_node = test.getQuestions(keys[j]);
			question[j]= question_node.Question;
		}

		List<Model> list = new ArrayList<Model>();
		for(int j = 0; j < question.length; j++){
			list.add(get(question[j]));
		}
		list.get(1).setSelected(true); // select one item by default
		return list;
	}
	private Model get(String s) {
		return new Model(s);
	}

}*/




/*public class MilestoneTracker extends ListActivity {

	String question[];
	String keys[];
	QuestionsObject test;
	Node question_node;


	public MilestoneTracker(){
		question = new String[10];
		// test = new QuestionsObject();
	}


	public void onCreate(Bundle buttons) {
		super.onCreate(buttons);
		setContentView(R.layout.activity_milestone_tracker);
		ArrayAdapter<Model> adapter = new ButtonAdapter(this, getModel());
		setListAdapter(adapter);

	}
	private List<Model> getModel() {

		SQLogic conn = new SQLogic(MilestoneTracker.this);
		conn.open();
		test= conn.RetrieveQuestions();
		conn.close();

		question = new String[test.count()];
		keys = new String[test.count()];
		test.getMap().keySet().toArray(keys);

		for (int j=0; j<test.count(); j++)  //not gauranteed that questionId's are in order
		{
			question_node = test.getQuestions(keys[j]);
			question[j]= question_node.Question;
		}

		List<Model> list = new ArrayList<Model>();
		for(int j = 0; j < question.length; j++){
			list.add(get(question[j]));
		}
		list.get(1).setSelected(true); // select one item by default
		return list;
	}
	private Model get(String s) {
		return new Model(s);
	}

}*/